import * as step from '@flow-step/step-toolkit'
import { IParams } from './params'

export async function getKubernetesCertificate(params: IParams) {
    if (params.kubernetesCluster == undefined) {
        throw new Error('missing kubernetesCluster')
    }

    if (!params.jobToken) {
        throw new Error('missing FLOW_JOB_TOKEN')
    }

    const kubernetesCertificate = await step.certificate.getKubernetesCertificate(params.jobToken!, params.kubernetesCluster as string)
    if (kubernetesCertificate === undefined || kubernetesCertificate.type === '') {
        throw new Error(`get KubernetesCertificate but error: ${JSON.stringify(kubernetesCertificate)}`)
    }

    step.debug(`get KubernetesCertificate data: ${JSON.stringify(kubernetesCertificate)}`)
    return Promise.resolve(kubernetesCertificate)
}